import sys

import pymongo
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QHBoxLayout, QComboBox, QSizePolicy

from ChartMaker.ChartMaker import ChartMaker

client = pymongo.MongoClient('localhost', 27017)
database = client['Weather']

db_region_list = database.list_collection_names()

info_dict = {}
for region in db_region_list:
    district_list = []
    for data in database[region].find():
        district_list.append(data['district'])
    info_dict[region] = district_list


class CityBox(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        layout = QVBoxLayout()

        # 城市选择框
        self.city_combobox = QComboBox()
        self.city_combobox.addItems(db_region_list)

        # 城市改变时要改变下级区选择框
        self.city_combobox.currentIndexChanged.connect(self.update_district_combobox)
        layout.addWidget(self.city_combobox)

        # 下级区选择框
        self.district_combobox = QComboBox()
        layout.addWidget(self.district_combobox)

        self.setLayout(layout)

    def update_district_combobox(self):
        # 改变下级区选择框
        city = self.city_combobox.currentText()
        self.district_combobox.clear()
        self.district_combobox.addItem(city)
        self.district_combobox.addItems(info_dict[city])


class Chart(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.layout = QHBoxLayout(self)

        # 初始使用欢迎界面
        self.myHtml = QWebEngineView()
        self.myHtml.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.myHtml.load(QUrl('file:///welcome.html'))

        self.layout.addWidget(self.myHtml)
        self.setLayout(self.layout)


class WeatherApp(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        self.setWindowTitle('Weather App')
        self.setGeometry(200,200,1200,800)
        layout = QVBoxLayout()

        self.city_box = CityBox()
        layout.addWidget(self.city_box)

        self.query_button = QPushButton('查询天气')
        self.query_button.clicked.connect(self.query_weather)
        layout.addWidget(self.query_button)

        self.chart = Chart()
        layout.addWidget(self.chart)

        self.setLayout(layout)

    def query_weather(self):
        # 获取查询的信息
        city = self.city_box.city_combobox.currentText()
        district = self.city_box.district_combobox.currentText()

        # 城市和下级区同名即查询全部
        if city == district:
            district = None

        # 绘制图表
        cm = ChartMaker()
        chart = cm.get_charts(city, district)
        chart.render(f'{chart.page_title}.html')
        with open(f'{chart.page_title}.html', 'rb') as f:
            content = f.read().decode('utf-8')

        # 修改显示图表
        self.chart.myHtml.setHtml(content)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    weather_app = WeatherApp()
    weather_app.show()
    sys.exit(app.exec_())
